package com.example.luckybag.sysservice

import android.R
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Build
import android.service.notification.NotificationListenerService
import android.service.notification.StatusBarNotification
import android.util.Log
import androidx.core.app.NotificationCompat
import com.example.luckybag.MainActivity
import com.example.luckybag.utils.Utils


/**
 * 文件名： <br/>
 * 描述：用于获取通知栏消息的 ，比如在桌面的时候，捕捉微信钉钉红包的消息，打开相应页面
 * 这个是因为部分机子用无障碍的形式无法收到通知栏变化的消息，故用此方法来替代🌝🌝🌝
 *
 * @author wulinran
 * @since 2023/2/6 14:44
 */
class MyNotificationListenerService : NotificationListenerService() {

    override fun onCreate() {
        super.onCreate()
        val ID = "com.example.luckybag" //这里的id里面输入自己的项目的包的路径

        val NAME = "MyNotificationListenerService"
        val intent = Intent(this@MyNotificationListenerService, MainActivity::class.java)
        val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_MUTABLE)
        val notification: NotificationCompat.Builder //创建服务对象

        val manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(ID, NAME, NotificationManager.IMPORTANCE_HIGH)
            channel.enableLights(true)
            channel.setShowBadge(true)
            channel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
            manager.createNotificationChannel(channel)
            notification =
                NotificationCompat.Builder(this@MyNotificationListenerService).setChannelId(ID)
        } else {
            notification = NotificationCompat.Builder(this@MyNotificationListenerService)
        }
        notification.setContentTitle("抢红包获取通知监听")
            .setContentText("瞧好吧您嘞")
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.mipmap.sym_def_app_icon)
            .setLargeIcon(BitmapFactory.decodeResource(resources, R.mipmap.sym_def_app_icon))
            .setContentIntent(pendingIntent)
            .build()
        val notification1 = notification.build()
        startForeground(12, notification1)
    }

    override fun onNotificationPosted(sbn: StatusBarNotification?) {
        super.onNotificationPosted(sbn)
        if(!Utils.luckyPackageSwitch()){
            return
        }
        val extras = sbn?.notification?.extras
        // 获取接收消息APP的包名
        val notificationPkg = sbn?.packageName
        // 获取接收消息的抬头
        val notificationTitle = extras?.getString(Notification.EXTRA_TITLE)
        // 获取接收消息的内容
        val notificationText = extras?.getString(Notification.EXTRA_TEXT)
        if (notificationPkg != null) {
            Log.d("收到的消息内容包名：", notificationPkg)
            if (notificationPkg == "com.tencent.mm") {
                if (notificationText?.contains("[微信红包]") == true) {
                    //收到微信红包了
                    Log.d("收到微信红包了：", notificationText)
                    val intent = sbn.notification.contentIntent
                    intent.send()
                }
            }
            if (notificationPkg == "com.alibaba.android.rimet") {
                if (notificationText?.contains("[红包]") == true) {
                    Log.d("收到钉钉红包了：", notificationText)
                    //收到钉钉红包了
                    val intent = sbn.notification.contentIntent
                    intent.send()
                }
            }
        }
        Log.d("收到的消息内容", "Notification posted $notificationTitle & $notificationText")
    }

    override fun onNotificationRemoved(sbn: StatusBarNotification?) {
        super.onNotificationRemoved(sbn)
    }
}